﻿using System;
using System.Collections.Generic;
using System.Linq;
using IllusionUtility.GetUtility;
using Unity.Linq;
using UnityEngine;

namespace Studio
{
	// Token: 0x02000468 RID: 1128
	public static class AddObjectItem
	{
		// Token: 0x06001C26 RID: 7206 RVA: 0x000B5884 File Offset: 0x000B3A84
		public static OCIItem Add(int _no)
		{
			int newIndex = Studio.GetNewIndex();
			Singleton<UndoRedoManager>.Instance.Do(new AddObjectCommand.AddItemCommand(_no, newIndex, Studio.optionSystem.initialPosition));
			return Studio.GetCtrlInfo(newIndex) as OCIItem;
		}

		// Token: 0x06001C27 RID: 7207 RVA: 0x000B58C0 File Offset: 0x000B3AC0
		public static OCIItem Load(OIItemInfo _info, ObjectCtrlInfo _parent, TreeNodeObject _parentNode)
		{
			ChangeAmount source = _info.changeAmount.Clone();
			OCIItem ociitem = AddObjectItem.Load(_info, _parent, _parentNode, false, -1);
			_info.changeAmount.Copy(source, true, true, true);
			AddObjectAssist.LoadChild(_info.child, ociitem, null);
			return ociitem;
		}

		// Token: 0x06001C28 RID: 7208 RVA: 0x000B5904 File Offset: 0x000B3B04
		public static OCIItem Load(OIItemInfo _info, ObjectCtrlInfo _parent, TreeNodeObject _parentNode, bool _addInfo, int _initialPosition)
		{
			OCIItem ociitem = new OCIItem();
			Info.ItemLoadInfo loadInfo = AddObjectItem.GetLoadInfo(_info.no);
			if (loadInfo == null)
			{
				loadInfo = AddObjectItem.GetLoadInfo(0);
			}
			ociitem.objectInfo = _info;
			GameObject gameObject = Utility.LoadAsset<GameObject>(loadInfo.bundlePath, loadInfo.fileName, loadInfo.manifest);
			if (gameObject == null)
			{
				GlobalData.Debug_LogError(string.Format("读取失败: {0} : {1} : {2}", loadInfo.manifest, loadInfo.bundlePath, loadInfo.fileName));
				Studio.DeleteIndex(_info.dicKey);
				return null;
			}
			gameObject.transform.SetParent(Singleton<Scene>.Instance.commonSpace.transform);
			ociitem.objectItem = gameObject;
			ociitem.arrayRender = (from v in gameObject.GetComponentsInChildren<Renderer>()
			where v.enabled
			select v).ToArray<Renderer>();
			ParticleSystem[] componentsInChildren = gameObject.GetComponentsInChildren<ParticleSystem>();
			if (!componentsInChildren.IsNullOrEmpty<ParticleSystem>())
			{
				ociitem.arrayParticle = (from v in componentsInChildren
				where v.isPlaying
				select v).ToArray<ParticleSystem>();
			}
			GuideObject guideObject = Singleton<GuideObjectManager>.Instance.Add(gameObject.transform, _info.dicKey);
			guideObject.isActive = false;
			guideObject.scaleSelect = 0.1f;
			guideObject.scaleRot = 0.05f;
			GuideObject guideObject2 = guideObject;
			guideObject2.isActiveFunc = (GuideObject.IsActiveFunc)Delegate.Combine(guideObject2.isActiveFunc, new GuideObject.IsActiveFunc(ociitem.OnSelect));
			guideObject.enableScale = loadInfo.isScale;
			ociitem.guideObject = guideObject;
			if (!loadInfo.childRoot.IsNullOrEmpty())
			{
				GameObject gameObject2 = gameObject.transform.FindLoop(loadInfo.childRoot);
				if (gameObject2)
				{
					ociitem.childRoot = gameObject2.transform;
				}
			}
			if (ociitem.childRoot == null)
			{
				ociitem.childRoot = gameObject.transform;
			}
			ociitem.animator = gameObject.GetComponent<Animator>();
			if (ociitem.animator)
			{
				ociitem.animator.enabled = loadInfo.isAnime;
			}
			if (loadInfo.isColor)
			{
				List<KeyValuePair<string, OCIItem.ColorTargetInfo>> listWork = AddObjectItem.GetTarget(gameObject, loadInfo.colorTarget);
				if (loadInfo.isColor2)
				{
					foreach (KeyValuePair<string, OCIItem.ColorTargetInfo> keyValuePair in from v in listWork
					where loadInfo.color2Target.Any((string s) => s == v.Key)
					select v)
					{
						keyValuePair.Value.isColor2 = true;
					}
					List<KeyValuePair<string, OCIItem.ColorTargetInfo>> target = AddObjectItem.GetTarget(gameObject, (from s in loadInfo.color2Target
					where !listWork.Any((KeyValuePair<string, OCIItem.ColorTargetInfo> v) => v.Key == s)
					select s).ToArray<string>());
					if (!target.IsNullOrEmpty<KeyValuePair<string, OCIItem.ColorTargetInfo>>())
					{
						foreach (KeyValuePair<string, OCIItem.ColorTargetInfo> keyValuePair2 in target)
						{
							keyValuePair2.Value.isColor2 = true;
							keyValuePair2.Value.isColor2Only = true;
						}
						listWork.AddRange(target);
					}
				}
				ociitem.colorTargets = (from v in listWork
				select v.Value).ToArray<OCIItem.ColorTargetInfo>();
			}
			if (_addInfo)
			{
				Studio.AddInfo(_info, ociitem);
			}
			else
			{
				Studio.AddObjectCtrlInfo(ociitem);
			}
			TreeNodeObject parent = (!(_parentNode != null)) ? ((_parent == null) ? null : _parent.treeNodeObject) : _parentNode;
			TreeNodeObject treeNodeObject = Studio.AddNode(loadInfo.name, parent);
			treeNodeObject.treeState = _info.treeState;
			TreeNodeObject treeNodeObject2 = treeNodeObject;
			treeNodeObject2.onVisible = (TreeNodeObject.OnVisibleFunc)Delegate.Combine(treeNodeObject2.onVisible, new TreeNodeObject.OnVisibleFunc(ociitem.OnVisible));
			treeNodeObject.enableVisible = true;
			treeNodeObject.visible = _info.visible;
			guideObject.guideSelect.treeNodeObject = treeNodeObject;
			ociitem.treeNodeObject = treeNodeObject;
			if (!loadInfo.bones.IsNullOrEmpty<string>())
			{
				ociitem.itemFKCtrl = gameObject.AddComponent<ItemFKCtrl>();
				ociitem.itemFKCtrl.InitBone(ociitem, loadInfo, _addInfo);
				ociitem.dynamicBones = ociitem.objectItem.GetComponentsInChildren<DynamicBone>();
			}
			else
			{
				ociitem.itemFKCtrl = null;
			}
			if (_initialPosition == 1)
			{
				_info.changeAmount.pos = Singleton<Studio>.Instance.cameraCtrl.targetPos;
			}
			_info.changeAmount.OnChange();
			Studio.AddCtrlInfo(ociitem);
			if (_parent != null)
			{
				_parent.OnLoadAttach((!(_parentNode != null)) ? _parent.treeNodeObject : _parentNode, ociitem);
			}
			if (ociitem.animator)
			{
				ociitem.animator.speed = _info.animeSpeed;
				if (_info.animeNormalizedTime != 0f && ociitem.animator.layerCount != 0)
				{
					ociitem.animator.Update(1f);
					AnimatorStateInfo currentAnimatorStateInfo = ociitem.animator.GetCurrentAnimatorStateInfo(0);
					ociitem.animator.Play(currentAnimatorStateInfo.shortNameHash, 0, _info.animeNormalizedTime);
				}
			}
			ociitem.UpdateColor();
			ociitem.ActiveFK(ociitem.itemInfo.enableFK);
			return ociitem;
		}

		// Token: 0x06001C29 RID: 7209 RVA: 0x000B5ED8 File Offset: 0x000B40D8
		private static List<KeyValuePair<string, OCIItem.ColorTargetInfo>> GetTarget(GameObject _obj, string[] _target)
		{
			if (_target.IsNullOrEmpty<string>())
			{
				return null;
			}
			return (from v in (from g in _obj.DescendantsAndSelf(null)
			where _target.Any((string s) => s == g.name)
			select g).OfComponent<Renderer>()
			select new KeyValuePair<string, OCIItem.ColorTargetInfo>(v.name, new OCIItem.ColorTargetInfo(v))).ToList<KeyValuePair<string, OCIItem.ColorTargetInfo>>();
		}

		// Token: 0x06001C2A RID: 7210 RVA: 0x000B5F50 File Offset: 0x000B4150
		private static Info.ItemLoadInfo GetLoadInfo(int _no)
		{
			Info.ItemLoadInfo result = null;
			if (!Singleton<Info>.Instance.dicItemLoadInfo.TryGetValue(_no, out result))
			{
				GlobalData.Debug_LogWarning(string.Format("不存在的号码[{0}]", _no));
				return null;
			}
			return result;
		}
	}
}
